<!--
  @author: cqz
  @desc: 数据源选择通用组件
  @date: 2020-6-29
-->
<template>
  <div>
    <el-tree :data="srcList"
             :props="srcListTreeProps"
             node-key="id"
             accordion
             style="max-height: 400px; overflow-y: auto"
             ref="srcList"
             @current-change="srcListTreeCurrentChangeHandle"
             :default-expand-all="false"
             :highlight-current="true"
             :expand-on-click-node="true">
    </el-tree>
  </div>
</template>

<script>
import {treeDataTranslate} from '../../../../utils/index'

export default {
  data () {
    return {
      srcList: [],
      srcListTreeProps: {
        label: 'name',
        children: 'children'
      }
    }
  },
  methods: {
    init (srcId) {
      this.$nextTick(() => {
        this.srcList = []
        this.srcTableList = []
        this.$http({
          url: this.$http.adornUrl(`/src/source/tree`),
          method: 'get'
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.srcList = treeDataTranslate(data.tree)
            let item = data.tree.find(item => (!item.children && item.id === srcId))
            if (item) {
              this.$emit('srcTreeSelectHandler', item)
            }
          }
        })
      })
    },
    // 菜单树选中
    srcListTreeCurrentChangeHandle (data, node) {
      if (node.isLeaf) {
        this.$emit('srcTreeSelectHandler', data)
      }
    }
  }
}
</script>

